import {mapGetters} from "vuex";
import * as GroupApi from '@/api/school/groupinfo.js';
import diyJson from '@/config/diy.js';
import {isBlank} from "@/utils/util";

export const mixins =
    {
      computed: mapGetters(['uid', 'userInfo', 'shareInfo', 'schoolId']),
      components: {
      },
      data() {
        return {
          themConfig: diyJson.commonStyle,
          tabCurrent: 0,
          groupComponentProperties: {
            "showTitle":false,
            "component": "schoolGroup",
            "text": "跑腿团活动",
            "type": "school_group",
            "active": false,
            "style": "schoolGroupStyle",
            "setStyle": {
              "text": "跑腿团活动",
              "jsonData": {
                "title": "跑腿团活动",
                "more": "更多",
                "pic": "",
                "titleType": "1",
                "button": "立即参与",
                "bgType": "1",
                "titleBgImg": "https://git-1304113371.cos.ap-nanjing.myqcloud.com/Snipaste_2024-09-14_19-06-58.png",
              },
              "pageMargin": 12,
              "buttonColor": "#cb6025",
              "progressColor": "#ea7724",
              "priceColor": "#ea7724",
              "borderRadius": 10,
              "timeColor": "rgba(254, 255, 255, 1)",
              "padding": 10,
              "backgroundColor": "rgba(255, 255, 255, 1)",
              "backgroundImage": "",
              "timeBgColor": "#ea7724"
            }
          },
          tabList:
              [{
                "name": "活动中",
                "value": 0
              },
                {
                  "name": "活动结束",
                  "value": 1
                }
              ],
          isReachBottom:
              false,
          loading:
              {
                status: 'nomore',
                loadingText:
                    '加载中',
                loadmoreText:
                    '点击加载更多',
              }
          ,
          groupInfos: [],
          dataPage:
              {
                nextId: undefined,
                hasNext:
                    false,
                ext:
                undefined
              }
          ,
          searchParams: {
            state: 0,
          }
          ,
          abortItem: {
            activityName: undefined,
            goods:
                {
                  saleNum: 0,
                  minNumber:
                      0,
                  maxNumber:
                      0,
                }
          }
          ,
        };
      },

      onLoad:

          function (options) {

          }

      ,

      created() {
      }
      ,

      onPullDownRefresh() {
        uni.stopPullDownRefresh();
      }
      ,

      onShow() {
        this.getGroupList(
            {...this.searchParams, schoolId: this.schoolId});
      }
      ,
      mounted() {
        // this.$refs.formPopup.open();
      }
      ,

      methods: {

        getGroupList(params) {
          GroupApi.myGroupInfos(params).then(res => {
            this.groupInfos = res.data.records;
            this.dataPage.ext = res.data.ext;
            this.dataPage.hasNext = res.data.hasNext;
            if (res.data.nextId) {
              this.dataPage.nextId = res.data.nextId;
            }
          });
        }
        ,
        // 滚到底部
        scrolltolower() {
          this.isReachBottom = true;
          this.loading.status = 'nomore';
          if (this.dataPage.hasNext) {
            this.loading.status = 'loadmore';
          }
        }
        ,
        loadmore() {
          if (this.dataPage.hasNext) {
            this.loading.status = 'loading';
            GroupApi.myGroupInfos({
              ...this.searchParams,
              schoolId: this.schoolId,
              nextId: this.dataPage.nextId
            }).then(res => {
              res.data.records.forEach(item => {
                this.groupInfos.push(item)
              })
              this.loading.status = 'loadmore';

              this.dataPage.ext = res.data.ext;
              this.dataPage.hasNext = res.data.hasNext;
              if (res.data.nextId) {
                this.dataPage.nextId = res.data.nextId;
              }
            });
          }
        }
        ,

        /**
         *  提交终止请求
         * @param type 1=弹出确认框
         * @param type 2=提交请求
         */
        confirmAbort(type, item) {
          if (item) {
            this.abortItem = item;
          }
          if (type === '1') {
            this.$refs.modal.open();
          } else {
            GroupApi.abortGroup({id: this.abortItem.activityBaseId}).then(
                res => {
                  this.getGroupList(
                      {...this.searchParams, schoolId: this.schoolId});
                });
          }
        }
        ,

        toDetail(item) {
          let params = encodeURIComponent(JSON.stringify(
              {'activityId': item.id, activityBaseId: item.activityBaseId}));
          uni.navigateTo({
            url: `/pages/users/my-group/detail/index?params=${params}`
          })
        }
        ,

        // 状态切换
        tabListChange(item) {
          // console.log('tabListChange',index)
          this.searchParams.state = item.value
          this.loading.status = 'nomore';
          this.getGroupList({...this.searchParams, schoolId: this.schoolId});
        }
        ,

        clickBtn() {
          if ((isBlank(this.userInfo.phone) || isBlank(this.schoolId))) {
            uni.showToast({
              title: '需要先注册信息......',
              duration: 1000,
              icon: 'none'
            });
            setTimeout(() => {
              uni.navigateTo({
                url: '/pages/users/user_info/index',
              })
            }, 500)
          } else {
            uni.navigateTo({url: '/pages/users/my-group/add/index'});
          }
        }
        ,

      }
      ,
      onReachBottom() {
        this.scrolltolower();
      }
    }
